package com.indexingsystem.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.indexingsystem.boss.admin.mapper.PusersMapper;
import com.indexingsystem.boss.admin.mapper.RoleGroupsMapper;
import com.indexingsystem.boss.admin.mapper.RolesMapper;
import com.indexingsystem.boss.admin.mapper.UserInfosMapper;
import com.indexingsystem.boss.admin.mapper.UserInstitutionMapper;
import com.indexingsystem.boss.admin.mapper.UserRoleMapper;
import com.indexingsystem.boss.common.PagedResult;
import com.indexingsystem.boss.entity.Pusers;
import com.indexingsystem.boss.entity.RoleGroups;
import com.indexingsystem.boss.entity.Roles;
import com.indexingsystem.boss.entity.UserInfos;
import com.indexingsystem.boss.entity.UserInstitution;
import com.indexingsystem.boss.entity.UserRole;
import com.indexingsystem.boss.logback.LogCvt;
import com.indexingsystem.boss.utils.BeanUtils;
import com.indexingsystem.db.mongoDB.impl.UserPublishMongo;
import com.indexingsystem.db.mongoDB.po.UserPublishMongoVo;
import com.indexingsystem.system.service.IUserRoleService;

@Service
public class UserRoleServiceImpl implements IUserRoleService {

	@Resource
	private UserRoleMapper			userRoleMapper;

	@Resource
	private UserInfosMapper			userInfosMapper;

	@Resource
	private UserInstitutionMapper	userInstitutionMapper;

	@Resource
	private RolesMapper				rolesMapper;

	@Resource
	private RoleGroupsMapper		roleGroupsMapper;

	@Resource
	private PusersMapper			pusersMapper;

	@Override
	public int deleteByPrimaryKey(String userRoleId) {
		return userRoleMapper.deleteByPrimaryKey( userRoleId );
	}

	@Override
	public int insertSelective(UserRole record) {
		return userRoleMapper.insertSelective( record );
	}

	@Override
	public UserRole selectByPrimaryKey(String userRoleId) {
		return userRoleMapper.selectByPrimaryKey( userRoleId );
	}

	@Override
	public int updateByPrimaryKeySelective(UserRole record) {
		return userRoleMapper.updateByPrimaryKeySelective( record );
	}

	@Override
	public PagedResult<UserRole> selectUserRoleList(Integer pageNo, Integer pageSize) {
		pageNo = pageNo == null ? 1 : pageNo;
		pageSize = pageSize == null ? 10 : pageSize;
		// startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		PageHelper.startPage( pageNo, pageSize );
		return BeanUtils.toPagedResult( userRoleMapper.selectUserRoleList() );
	}

	/**
	 * 通过角色id查询角色包含用户列表.
	 */
	@Override
	public PagedResult<UserRole> selectUserListByRoleId(String roleId, Integer pageNo, Integer pageSize) {
		pageNo = pageNo == null ? 1 : pageNo;
		pageSize = pageSize == null ? 10 : pageSize;
		// startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		PageHelper.startPage( pageNo, pageSize );
		return BeanUtils.toPagedResult( userRoleMapper.selectUserListByRoleId( roleId ) );
	}

	/**
	 * 获取角色包含用户总列表总数.
	 * 
	 * @return
	 */
	@Override
	public Integer getUserRoleListCount(String roleId) {
		return userRoleMapper.getUserRoleListCount( roleId );
	}

	/**
	 * 批量新增用户角色关系信息.
	 * 
	 * @param userRoleList
	 * @return
	 */
	@Override
	public int addUserRolesBatch(List<UserRole> userRoleList) {
		Integer result = 0;
		if (null != userRoleList && userRoleList.size() > 0) {
			result += userRoleMapper.addUserRolesBatch( userRoleList );

			// 更新mongo用户信息
			for (UserRole userRole : userRoleList) {
				updateUserInfoMongo( userRole.getUserId() );
			}
		}
		return result;
	}

	/**
	 * 角色关联时，同步更新mongo用户信息.
	 * 
	 * @param userId
	 */
	private void updateUserInfoMongo(String userId) {
		// 先删mongo 后加
		List<String> userIdList = new ArrayList<String>();
		userIdList.add( userId );
		UserPublishMongo.deleteUserByIds( userIdList );

		UserPublishMongoVo userPublishMongoVo = getUserPublishMongoByUserId( userId );
		try {
			if (null != userPublishMongoVo) {
				UserPublishMongo.addUser( userPublishMongoVo );
			}
		} catch (Exception ex) {
			LogCvt.error( ex.getMessage(), ex );
		}

	}

	/**
	 * 通过用户id获取用户缓存信息对象.
	 * 
	 * @param userId
	 * @return
	 */
	private UserPublishMongoVo getUserPublishMongoByUserId(String userId) {
		UserPublishMongoVo userPublishMongoVo = new UserPublishMongoVo();

		// 1、获取用户信息
		Pusers pusers = pusersMapper.selectPusersInfoList( userId );
		if (null != pusers) {
			userPublishMongoVo.setUserId( pusers.getUserId() );
			userPublishMongoVo.setPusers( pusers );

			// 2、获取用户详情信息
			UserInfos userInfos = userInfosMapper.selectUserInfoByUserId( userId );
			userPublishMongoVo.setUserInfos( userInfos );

			// 3、获取用户角色列表
			List<UserRole> userRoleList = userRoleMapper.getUserRoleList( userId );
			userPublishMongoVo.setUserRoleList( userRoleList );

			// 4、获取角色信息列表
			List<Roles> rolesList = new ArrayList<Roles>();

			// 5、角色组列表
			List<RoleGroups> roleGroupsList = new ArrayList<RoleGroups>();
			if (null != userRoleList && userRoleList.size() > 0) {
				for (UserRole userRole : userRoleList) {
					Roles queryRoles = rolesMapper.selectByPrimaryKey( userRole.getRoleId() );
					if (null != queryRoles) {
						if (!rolesList.contains( queryRoles )) {
							rolesList.add( queryRoles );
						}

						RoleGroups queryRoleGroups = roleGroupsMapper.selectByPrimaryKey( queryRoles.getRoleGroupId() );
						if (null != queryRoleGroups) {
							if (!roleGroupsList.contains( queryRoleGroups )) {
								roleGroupsList.add( queryRoleGroups );
							}
						}
					}
				}
			}

			userPublishMongoVo.setRolesList( rolesList );
			userPublishMongoVo.setRoleGroupsList( roleGroupsList );

			// 获取用户机构信息
			UserInstitution queryUserInstitution = userInstitutionMapper.selectUserInstitutionByUserId( userId );
			userPublishMongoVo.setUserInstitution( queryUserInstitution );

		}

		return userPublishMongoVo;
	}

	/**
	 * 批量删除用户角色关系信息.
	 * 
	 * @param userRoleIdList
	 * @return
	 */
	@Override
	public int deleteUserRolesBatch(List<String> userRoleIdList) {
		Integer result = 0;
		if (null != userRoleIdList && userRoleIdList.size() > 0) {
			result += userRoleMapper.deleteUserRolesBatch( userRoleIdList );
			// 更新mongo用户信息
			for (String userRoleId : userRoleIdList) {
				// 通过用户角色id获取用户id
				UserRole userRole = userRoleMapper.selectByPrimaryKey( userRoleId );
				if (null == userRole) {
					continue;
				}

				String userId = userRole.getUserId();
				updateUserInfoMongo( userId );
			}
		}
		return result;
	}

	@Override
	public UserRole selectByPrimaryUserId(String userId) {
		return userRoleMapper.selectByPrimaryUserId( userId );
	}

	@Override
	public List<UserRole> getDhUserRoleList(String userId) {
		// TODO Auto-generated method stub
		return userRoleMapper.getDhUserRoleList( userId );
	}
}